package com.syh.jn.scm.domain.operation.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.other.ExtStringUtils;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.operation.repository.system.PermissionRepository;
import com.syh.jn.scm.domain.operation.repository.system.PermissionTypeRepository;
import com.syh.jn.scm.dto.operation.system.permission.PermissionTypeMto;
import com.syh.jn.scm.dto.operation.system.permission.PermissionTypeSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Arrays;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
public class PermissionType extends AbstractSimpleAggregate<Long> {

  public PermissionType() {
    this.del = Short.valueOf("1");
  }

  public PermissionType(PermissionTypeSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);
    ExtBeanUtils.copyProperties(sto, this, false);

    if (this.superId == null && ExtStringUtils.isEmpty(this.terminalType)) {
      throw new BusinessException("终端类型不能为空");
    }

    // 子类权限模块应该与父类一致 且不可修改
    if (this.superId != null) {
      PermissionTypeRepository permissionTypeRepository = (PermissionTypeRepository) SpringContextUtils.getBean(PermissionTypeRepository.class);
      PermissionType superType = permissionTypeRepository.getById(this.superId);
      if (superType == null) {
        throw new BusinessException("父类不存在");
      }
      this.terminalType = superType.terminalType;
    } else {
      List<String> checkStringList = Arrays.asList("DOWNSTREAM", "OPERATION", "SUPPLIER", "UPSTREAM");
      if (!checkStringList.contains(this.terminalType)) {
        throw new BusinessException("终端类型不合法");
      }
      this.superId = 0L;
    }
    this.del = Short.valueOf("1");
    this.orderLevel = this.orderLevel == null ? 1 : this.orderLevel;


  }

  public void modify(PermissionTypeMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);
    this.del = Short.valueOf("1");
    ExtBeanUtils.copyProperties(mto, this, false);
  }

  public void destory() {
    Wrapper wrapper = Wrapper.build();
    wrapper.append(
            Restrictions.and(Restrictions.eq("typeId", this.id))
    );
    PermissionRepository permissionRepository = (PermissionRepository) SpringContextUtils.getBean(PermissionRepository.class);
    List list = permissionRepository.getList(wrapper);
    if (list != null && list.size() > 0) {
      throw new BusinessException("该权限类型下有权限存在,不能删除");
    }
  }

  private Long superId;
  /**
   * 权限级别名称
   */
  private String permissionTypeName;
  /**
   * 所属终端系统:DOWNSTREAM,OPERATION,SUPPLIER,UPSTREAM
   */
  private String terminalType;
  /**
   * 父类目录    格式：1-11  1：父父类id，11：父类id
   */
  private String catalogCode;
  /**
   * 排序
   */
  private Short orderLevel;
  /**
   * 模块描述
   */
  private String description;

  private Short del;

}

